<template>
  <view class="cart-container" v-if="cart.length !== 0">
    <!-- 收获地址组件 -->
    <my-address></my-address>
    
    <!-- 购物车商品列表的标题区域 -->
    <view class="cart-title">
      <uni-icons type="shop" size="18"></uni-icons>
      <text class="cart-title-text">购物车</text>
    </view>
    
    <!-- 商品列表区域 -->
    <!-- uni-swipe-action-item 可以为其子节点提供滑动操作的效果。需要通过options 属性来指定操作按钮的配置信息-->
    <uni-swipe-action>
      <block v-for="(goods,index) in cart" :key="index">
        <uni-swipe-action-item :right-options="options" @click="swipeItemClickHandler(goods)">
          <my-goods :goods='goods' :show-radio="true" @radio-change="radioChangeHandler" :show-num="true" @num-change="numberChangeHandler"></my-goods>
        </uni-swipe-action-item>
      </block>
      
    </uni-swipe-action>
    
    <!-- 商品结算区域 -->
    <my-settle></my-settle>
  </view>
  
  <view  class="empty-cart" v-else>
    <image src="/static/cart_empty@2x.png" class="empty-img"></image>
    <text class="empty-text">空空如也</text>
  </view>
</template>

<script>
  // 导入自己封装的 mixin 模块
 import badgeMix from '../../mixins/tabbar-badge.js'
 import { mapState, mapMutations } from 'vuex'
  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [badgeMix],
    computed:{
      ...mapState('cart',['cart'])
    },
    data() {
      return {
        options:[
          {
            text: '删除',
            style: {
              backgroundColor: '#c00000'
            }
          }
        ]
      };
    },
    methods:{
      // 选择状态得值在store下得cart.js中，不能直接修改,需要修改state中得值，需要在mutations中定义方法才能修改
      ...mapMutations('cart',['updateGoodsState','updateGoodsCount','removeGoodsById']),
      // 商品的勾选状态发生了变化
      radioChangeHandler(e){
        this.updateGoodsState(e)
      },
      // 商品的数量发生了变化
      numberChangeHandler(e){
        this.updateGoodsCount(e)
      },
      // 点击了滑动操作按钮
      swipeItemClickHandler(goods){
        console.log(goods)
        this.removeGoodsById(goods.goods_id)
      }
    }
  }
</script>

<style lang="scss">
  .cart-container{
    padding-bottom: 50px;
  }
.cart-title{
  height: 40px;
  display: flex;
  align-items: center;
  font-size: 14px;
  padding-left: 5px;
  border: 1px solid #efefef;
  .cart-title-text{
    margin-left: 10px;
  }
}
.empty-cart{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding-top: 200px;
  .empty-img{
    width: 120px;
    height: 120px;
  }
  .empty-text{
    font-size: 16px;
    color: grey;
    margin-top: 15px;
  }
}
</style>
